import pandas as pd
import re,json
def create_time(item):
    return str(item['create_time'])
def reservation_startTime(item):
    return str(item['reservation_startTime'])

def reservation_endTime(item):
    return str(item['reservation_endTime'])


def goodsBeginTime(item):
    return str(item['goodsBeginTime'])

def goodsEndingTime(item):
    return str(item['goodsEndingTime'])

def ctime(item):
    return str(item['ctime'])


def date(item):
    return str(item['date'])

def startTime(item):
    return str(item['startTime'])

def endTime(item):
    return str(item['endTime'])

def utime(item):
    return str(item['utime'])

def register_date(item):
    return str(item['register_date'])
    # print()
def goodsStatus(item):
    if(item['goodsStatus'] == '1'):
        return True
    else:
        return False
def stream(item):
    if(item['stream'] == '1' or item['stream'] == 1):
        return True
    else:
        return False
def status(item):
    if(item['status'] == 'normal' or item['status'] == '1' or item['status'] == 1):
        return True
    else:
        return False
def sign_in_send(item):
    if(item['sign_in_send'] == 'normal' or item['sign_in_send'] == '1' or item['sign_in_send'] == 1):
        return True
    else:
        return False
def enable(item):
    if(item['enable'] == 'normal'):
        return True
    else:
        return False
def extJson(item):
    if(item['extJson'] == '1'):
        return True
    else:
        return False
def showLink(item):
    if(item['showLink'] == 1):
        return True
    else:
        return False
def showParent(item):
    if(item['showParent'] == 1):
        return True
    else:
        return False
def frameLoading(item):
    if(item['frameLoading'] == 1):
        return True
    else:
        return False
def hiddenTag(item):
    if(item['hiddenTag'] == 1):
        return True
    else:
        return False
def keepAlive(item):
    if(item['keepAlive'] == 1 or item['keepAlive'] == "1"):
        return True
    else:
        return False
def fixedTag(item):
    if(item['fixedTag'] == 1):
        return True
    else:
        return False

def to_integers(lst): # 讲数组 字符串全变整数
    return [int(item) if item.isdigit() else 0 for item in lst]

def menu_list(item):

    if item['menu_list']:
        list1 = item['menu_list'].split(',')
        return to_integers(list1)
    else:
        return []

def tabbar(item):
    if item['tabbar']:
        list1 = eval(item['tabbar'])
        return list1
    else:
        return []
# 生成json数据
def ProJosn(data,sql=None,type='sql',groupType=None):
    if data:
        sql1 = sql
        if type == 'sql':
            sql1 = re.findall(f'select (.*?) from',sql)[0].split(',')  # form跟  跟后面的  sql * from 这个只能一条线 不能换行  不然识别不到
        Table = pd.DataFrame(data, columns=sql1)

        if groupType == 'sum':
            Table = Table.groupby(['id','nickName','openid','head_img','phone','create_time'])['money'].sum().reset_index()
        if groupType == 'orderSum':
            Table = Table.groupby(['nickName', 'openid', 'head_img', 'phone'])['money'].sum().reset_index()

        # dataS = {
        #     'create_time': Table.apply(create_time, axis=1),
        #     'ctime': Table.apply(ctime, axis=1),
        #     'date': Table.apply(date, axis=1),
        #     'register_date': Table.apply(register_date, axis=1),
        #     'startTime': Table.apply(startTime, axis=1),
        #     'endTime': Table.apply(endTime, axis=1),
        #     'utime': Table.apply(utime, axis=1),
        #     'goodsBeginTime': Table.apply(goodsBeginTime, axis=1),
        #     'goodsEndingTime': Table.apply(goodsEndingTime, axis=1),
        #     'goodsStatus': Table.apply(goodsStatus, axis=1),
        #     'status': Table.apply(status, axis=1),
        #     'extJson': Table.apply(extJson, axis=1),
        #     'showLink': Table.apply(showLink, axis=1),
        #     'showParent': Table.apply(showParent, axis=1),
        #     'enable': Table.apply(enable, axis=1),
        #     'hiddenTag': Table.apply(hiddenTag, axis=1),
        #     'keepAlive': Table.apply(keepAlive, axis=1),
        #     'fixedTag': Table.apply(fixedTag, axis=1),
        #     'menu_list': Table.apply(menu_list, axis=1),
        #     'tabbar': Table.apply(tabbar, axis=1),
        #     'stream': Table.apply(stream, axis=1),
        # }
        # print(1111,list(dataS))
        for i in sql1:
            # for j in list(dataS):
            #     print(j)
            #     Table[f'{i}'] = dataS[f'{i}']
            # try:
            #     Table[f'{i}'] = dataS[f'{i}']
            #     continue
            # except Exception as error:
            #     continue
            if (i == 'create_time'):
                Table['create_time'] = Table.apply(create_time, axis=1)
                continue
            elif (i == 'ctime'):
                Table['ctime'] = Table.apply(ctime, axis=1)
                continue
            elif (i == 'date'):
                Table['date'] = Table.apply(date, axis=1)
                continue
            elif (i == 'register_date'):
                Table['register_date'] = Table.apply(register_date, axis=1)
                continue
            elif (i == 'startTime'):
                Table['startTime'] = Table.apply(startTime, axis=1)
                continue
            elif (i == 'endTime'):
                Table['endTime'] = Table.apply(endTime, axis=1)
                continue
            elif (i == 'utime'):
                Table['utime'] = Table.apply(utime, axis=1)
                continue
            elif (i == 'goodsBeginTime'):
                Table['goodsBeginTime'] = Table.apply(goodsBeginTime, axis=1)
                continue
            elif (i == 'goodsEndingTime'):
                Table['goodsEndingTime'] = Table.apply(goodsEndingTime, axis=1)
                continue
            elif (i == 'goodsStatus'):
                Table['goodsStatus'] = Table.apply(goodsStatus, axis=1)
                continue
            elif (i == 'status'):
                Table['status'] = Table.apply(status, axis=1)
                continue
            elif (i == 'sign_in_send'):
                Table['sign_in_send'] = Table.apply(status, axis=1)
                continue
            elif (i == 'extJson'):
                Table['extJson'] = Table.apply(extJson, axis=1)
                continue
            elif (i == 'showLink'):
                Table['showLink'] = Table.apply(showLink, axis=1)
                continue
            elif (i == 'showParent'):
                Table['showParent'] = Table.apply(showParent, axis=1)
                continue
            elif (i == 'enable'):
                Table['enable'] = Table.apply(enable, axis=1)
                continue
            elif (i == 'hiddenTag'):
                Table['hiddenTag'] = Table.apply(hiddenTag, axis=1)
                continue
            elif (i == 'keepAlive'):
                Table['keepAlive'] = Table.apply(keepAlive, axis=1)
                continue
            elif (i == 'fixedTag'):
                Table['fixedTag'] = Table.apply(fixedTag, axis=1)
                continue

            elif (i == 'menu_list'):
                Table['menu_list'] = Table.apply(menu_list, axis=1)
                continue
            elif (i == 'tabbar'):
                Table['tabbar'] = Table.apply(tabbar, axis=1)
                continue
            elif (i == 'stream'):
                Table['stream'] = Table.apply(stream, axis=1)
                continue
            elif (i == 'reservation_endTime'):
                Table['reservation_endTime'] = Table.apply(reservation_endTime, axis=1)
                continue
            elif (i == 'reservation_startTime'):
                Table['reservation_startTime'] = Table.apply(reservation_startTime, axis=1)
                continue

        return Table.to_dict(orient='records')

    else:
        return []

def sql_qeury(sql,params):
    num = 0

    for i in list(params):

        if num== 0:
            sql += f"""
                 {i}={"'"+params[i]+"'"}
            """
        else:
            sql += f"""
                and {i}={"'" + params[i] + "'"}
            """
        num += 1


    return sql


